﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.IO;

namespace DotNet.Common.Kit
{
    /// <summary>
    /// 有关系统操作的类，如关机，打开浏览器等。
    /// </summary>
    public class SysOperCls
    {
        LogerHelper log = new LogerHelper(DirType.Local, MessageType.Error);
        public static void CloseComputerSystem()
        {
            // ConnectionOptions BootConn = new ConnectionOptions();
            // BootConn.Username = "";
            // BootConn.Password = "";
            //string computerName=ReadIniFile.IniReadValue("closeCoumpter", "computerName");
            string computerName = System.Net.Dns.GetHostName();
            ManagementScope ms = new ManagementScope("\\\\" + computerName + "\\root\\cimv2");
            ms.Options.EnablePrivileges = true;
            try
            {
                ms.Connect();
            }
            catch (Exception err)
            {
                
            }
            if (ms.IsConnected)
            {
                try
                {
                    ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                    ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
                    ManagementObjectCollection moc = mos.Get();
                    foreach (ManagementObject mo in moc)
                    {
                        string[] ss = { "8", "4" };
                        mo.InvokeMethod("Win32Shutdown", ss);
                    }
                }
                catch (Exception ex)
                {
                    
                    //MessageBox.Show(ex.Message);
                }
            }
        }

        public static void OpenBrowser(string url)
        {
            Process process=Process.Start("iexplore.exe", url);
        }
    }
}
